﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Just.Library.Extensions;
using Just.Library.Util;
using Newtonsoft.Json;

namespace ConsoleAppFramework.Util
{
    /// <summary>
    /// 飞书自定义机器人，发送消息的帮助类
    /// </summary>
    public static class FeiShuMsgHelper
    {
        /// <summary>
        /// 异常机器人Webhooks
        /// </summary>
        public static List<string> Error_Webhooks
        {
            get
            {
                var webhooks = new List<string>();
                string default_webhook = "https://open.feishu.cn/open-apis/bot/v2/hook/c5c8d854-5fe8-4b42-8be6-39d06fa40704";
                webhooks.Add(default_webhook);
                var s = ConfigHelper.GetString("Error_Webhooks");
                if (!string.IsNullOrEmpty(s))
                {
                    webhooks = s.Split(',').ToList();
                }
                return webhooks;
            }
        }

        /// <summary>
        /// 正常通知机器人
        /// </summary>
        public static List<string> Webhooks
        {
            get
            {
                var webhooks = new List<string>();
                string default_webhook = "https://open.feishu.cn/open-apis/bot/v2/hook/c5c8d854-5fe8-4b42-8be6-39d06fa40704";
                webhooks.Add(default_webhook);
                var s = ConfigHelper.GetString("Webhooks");
                if (!string.IsNullOrEmpty(s))
                {
                    webhooks = s.Split(',').ToList();
                }
                return webhooks;
            }
        }
        public static void SendMsg<T>(string webhook, FeiShuMsgTypeEnum msg_type, T content)
        {
            SendMsg(new List<string> { webhook }, msg_type, content);
        }
        public static void SendMsg<T>(List<string> webhooks, FeiShuMsgTypeEnum msg_type, T content)
        {
            if (webhooks == null || webhooks.Count == 0) return;
            //if (string.IsNullOrEmpty(contentData)) return;
            var postModel = new FeiShuMsgContent<T>();
            postModel.msg_type = msg_type.ToString();
            postModel.content = content;
            var postDataStr = JsonConvert.SerializeObject(postModel);
            ///AsParallel().ForAll
            webhooks.ForEach(webhook =>
            {
                if (string.IsNullOrWhiteSpace(webhook))
                {
                    return;
                }
                try
                {
                    var response = HttpHelper.PostAsync(webhook, postDataStr).Result;
                }
                catch (Exception ex)
                {
                    //new NLogSeqLogger("【飞书消息通知】").WriteErrorLog($"发送消息失败,参数:{webhook},postDataStr:{postDataStr}", ex);
                }
            });
        }
        public static void SendTextMsg(List<string> webhooks, string text_msg)
        {
            var content = new FeiShuTextContent() { text = text_msg };
            SendMsg(webhooks, FeiShuMsgTypeEnum.text, content);
        }

        public static void SendTextMsg(string webhook, string text_msg)
        {
            var content = new FeiShuTextContent() { text = text_msg };
            SendMsg(webhook, FeiShuMsgTypeEnum.text, content);
        }

        /// <summary>
        /// SendGroupMsg
        /// </summary>
        /// <param name="feiShuGroupEnum"></param>
        /// <param name="text_msg"></param>
        public static void SendGroupMsg(FeiShuGroupEnum feiShuGroupEnum,string text_msg)
        {
            var content = new FeiShuTextContent() { text = text_msg };
            SendGroupMsg(FeiShuMsgTypeEnum.text, feiShuGroupEnum, content);
        }

        /// <summary>
        /// SendGroupMsg
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="feiShuGroupEnum">指定群</param>
        public static void SendGroupMsg<T>(FeiShuMsgTypeEnum msg_type, FeiShuGroupEnum feiShuGroupEnum, T content)
        {
            var url = feiShuGroupEnum.GetDescription();
            var postModel = new FeiShuMsgContent<T>();
            postModel.msg_type = msg_type.ToString();
            postModel.content = content;
            var postDataStr = JsonConvert.SerializeObject(postModel);
            try
            {
                var response = HttpHelper.PostAsync(url, postDataStr).Result;
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteErrorLine($"发送{feiShuGroupEnum}错误" + ex.ToString());
            }
        }
    }

    public class FeiShuMsgContent<T>
    {
        public string msg_type { get; set; }
        public T content { get; set; }
    }

    public class FeiShuTextContent
    {
        public string text { get; set; }
    }

    #region 发送富文本消息实体

    public class ContentItemItem
    {
        /// <summary>
        /// 
        /// </summary>
        public string tag { get; set; }
        /// <summary>
        ///  
        /// </summary>
        public string text { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string href { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string user_id { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string email { get; set; }

    }

    public class Zh_cn
    {
        /// <summary>
        /// 项目更新通知
        /// </summary>
        public string title { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<List<ContentItemItem>> content { get; set; }
    }

    public class Post
    {
        /// <summary>
        /// 
        /// </summary>
        public Zh_cn zh_cn { get; set; }
    }

    public class FeiShuPostContent
    {
        /// <summary>
        /// 
        /// </summary>
        public Post post { get; set; }
    }

    #endregion

    #region 发生卡片消息实体

    public class Text
    {
        /// <summary>
        /// 
        /// </summary>
        public string content { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string tag { get; set; }
    }

    public class ElementsItem
    {
        /// <summary>
        /// 
        /// </summary>
        public string tag { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Text text { get; set; }
    }

    public class Title
    {
        /// <summary>
        /// 
        /// </summary>
        public string content { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string tag { get; set; }
    }

    public class Header
    {
        /// <summary>
        /// 
        /// </summary>
        public Title title { get; set; }
    }

    public class Card
    {
        /// <summary>
        /// 
        /// </summary>
        public List<ElementsItem> elements { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Header header { get; set; }
    }

    #endregion

    public enum FeiShuMsgTypeEnum
    {
        text,
        post,
        image,
        share_chat,
        interactive
    }

    public enum FeiShuGroupEnum
    {
        [Description("https://open.feishu.cn/open-apis/bot/v2/hook/c5c8d854-5fe8-4b42-8be6-39d06fa40704")]
        测试 = 1,
    }

    public class FeiShuGroupMsg
    {
        public string Content { get; set; }
        public FeiShuGroupEnum FeiShuGroupEnum { get; set; }
    }
}
